package simplyrugby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import simplyrugby.common.LoginContext;
import simplyrugby.entity.User;
import simplyrugby.mapper.*;
import simplyrugby.service.UserService;
import simplyrugby.utils.SecurityUtils;

import java.util.List;

/**
 * @author admin
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-03-29 18:36:05
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final PositionMapper positionMapper;
    private final RoleMapper roleMapper;
    private final MenuMapper menuMapper;
    private final TeamMapper teamMapper;

    @Override
    public String login(User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword());
        User one = this.getOne(wrapper);

        if (ObjectUtils.isEmpty(one)) {
            throw new RuntimeException("Error username or password.");
        }


        return SecurityUtils.encrypt(one.getId());
    }

    @Override
    public boolean exist(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = this.getOne(wrapper);
        return !ObjectUtils.isEmpty(user);
    }

    @Override
    public Page<User> list(User user, Integer pageNum, Integer pageSize) {

        User loginUser = baseMapper.selectById(LoginContext.getLoginUserId());

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class)
                .eq(!ObjectUtils.isEmpty(user.getId()), User::getId, user.getId())
                .like(!ObjectUtils.isEmpty(user.getUsername()), User::getUsername, user.getUsername())
                .like(!ObjectUtils.isEmpty(user.getFirstname()), User::getFirstname, user.getFirstname())
                .like(!ObjectUtils.isEmpty(user.getLastname()), User::getLastname, user.getLastname())
                .eq(!ObjectUtils.isEmpty(user.getRoleId()), User::getRoleId, user.getRoleId())
                .eq(!ObjectUtils.isEmpty(loginUser.getTeamId()), User::getTeamId, loginUser.getTeamId())
                .eq(!ObjectUtils.isEmpty(user.getPosition()), User::getPosition, user.getPosition());

        Page<User> page = new Page<>(pageNum, pageSize);
        this.page(page, wrapper);
        List<User> list = page.getRecords().stream().map(u -> this.details(u.getId())).toList();
        page.setRecords(list);
        return page;
    }

    @Override
    public User details(String id) {
        User user = baseMapper.selectById(id);
        if (!ObjectUtils.isEmpty(user)) {
            user.setPosition(positionMapper.selectById(user.getPositionId()));
            user.setRole(roleMapper.selectById(user.getRoleId()));

            if (user.getTeamId() != null) {

                user.setTeam(teamMapper.selectById(user.getTeamId()));
            }


            if ("1".equals(user.getRoleId())) {
                user.setMenuList(menuMapper.selectList(null));
            } else {
                user.setMenuList(menuMapper.selectListByRoleId(user.getRoleId()));
            }
        }
        return user;
    }
}




